using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;

namespace Moogle
{
    /// <summary>
    /// PopTime List Adapter class. This class extends the Android.BaseAdapter class, which can 
    /// be used to display the contents of a collection (List) of PopTime objects.  Its
    /// target consumer is the ActivityPopTimeEdit class.
    /// </summary>
    public class PopTimeListAdapter : BaseAdapter<PopTime>
    {

        #region Local Memory
      
        /// <summary>
        /// Constant using to identify (filter) entries created in the
        /// Android Log by this class.
        /// </summary>
        private const string TAG = "PopTimeListAdapter";
        Activity context;
        /// <summary>
        /// PopTimes Collection Accessor.
        /// </summary>
        public List<PopTime> PopTimes;
        #endregion
        #region Class Constructor
        /// <summary>
        /// Class Constructor.
        /// </summary>
        /// <param name="context">Activity context instance</param>
        /// <param name="PopTimes">List collection containing all PopTimes to be displayed by the adapter</param>
        public PopTimeListAdapter(Activity context, List<PopTime> PopTimes)
            : base()
        {
            this.context = context;
            this.PopTimes = PopTimes;
            Log.Debug(TAG, "PopTime List Adapter created.");

        }
        #endregion
        #region Base Class Overrides
        public override int Count
        {
            get { return this.PopTimes.Count; }
        }

        public override PopTime this[int position]
        {
            get { return this.PopTimes[position]; }
        }
        /// <summary>
        /// GetView Override.
        /// </summary>
        /// <param name="position">integer value defining the item location in the list</param>
        /// <param name="convertView">view object instance</param>
        /// <param name="parent">view group object instance</param>
        /// <returns></returns>
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            //Get our object for this position
            var item = this.PopTimes[position];

            /// Try to reuse convertView if it's not  null, otherwise inflate it from our item layout
            /// This gives us some performance gains by not always inflating a new view
            /// This will sound familiar to MonoTouch developers with UITableViewCell.DequeueReusableCell()
            var view = convertView;

            if (convertView == null || !(convertView is LinearLayout))
                view = context.LayoutInflater.Inflate(Resource.Layout.list_item_time, parent, false);

            /// Find references to each subview in the list item's view
            var textViewPop = view.FindViewById(Resource.Id.timeDisplay) as Android.Widget.TextView;

            
            /// Assign this item's values to the various subviews
            textViewPop.SetText(PopTime.TimeStamp(item.Hour,item.Minute,item.AnteMeridiem), TextView.BufferType.Normal);

            Log.Debug(TAG, "Adding Button Event Listener...");

            /// Find references to each subview in the list item's view
            var buttonPick = view.FindViewById(Resource.Id.pickTime) as Android.Widget.Button;

            /// Wire the button click event handler
            buttonPick.Click += delegate
            {
                var spawn = new TimePickerDialog(context,
                    (object sender, TimePickerDialog.TimeSetEventArgs e) => 
                    {
                        Boolean _booleanAM = false;
                        Boolean _booleanPM = true;

                        if (e.HourOfDay < 12)
                        {
                            _booleanAM = true;
                            _booleanPM = false;
                        } // end if

                        /// Build timestamp based on the list item 
                        var _timeStamp = PopTime.TimeStamp(e.HourOfDay, e.Minute, _booleanAM);
                        /// Assign this item's values to the various subviews
                        textViewPop.SetText(_timeStamp, TextView.BufferType.Normal);
                        /// Loop through the PopTimes list, 
                        /// If the Medication Name matches the 
                        /// current item, then reset the Hour/Minutes/AnteMeridiem properties
                        /// based on the value returned by the time edit dialog
                        foreach (PopTime _pop in PopTimes)
                        {
                            if (_pop.MedicationName == item.MedicationName)
                            {
                                _pop.Hour = e.HourOfDay;
                                _pop.Minute = e.Minute;
                                _pop.AnteMeridiem = _booleanAM;
                                _pop.PostMeridiem = _booleanPM;
                            } // end if
                        } // end foreach
                    } // end callback
                    , item.Hour, item.Minute, false);
                spawn.Show();
            }; // end delegate

            return view;
        }
       
        public override long GetItemId(int position)
        {
            return position;
        }
        #endregion
       
    }

}